package router

import (
	"4-26zk/client/client"
	"4-26zk/client/requset"
	"4-26zk/client/response"
	"4-26zk/comment/pkg"
	"4-26zk/comment/proto/product"
	"github.com/gin-gonic/gin"
	"path/filepath"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		response.ResPonseError(c, err.Error())
		return
	}
	if file.Size >= 5*1024*1024 {
		response.ResPonseError(c, "文件过大")
		return
	}
	ext := filepath.Ext(file.Filename)
	m := map[string]bool{
		".png": true,
		".jpg": true,
		".mp4": true,
	}
	if !m[ext] {
		response.ResPonseError(c, "文件类型错误")
		return
	}
	dst := "./image" + ext
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		return
	}
	pkg.Upload(dst, file.Filename)
	response.ResPonseSuccess(c, "上传成功"+pkg.GetUrl(file.Filename))
}

func ProductCreate(c *gin.Context) {
	var data requset.ProductCreate
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResPonseError(c, err.Error())
		return
	}
	create, err := client.ProductCreate(c, &product.ProductCreateReq{
		CategoryId:         int64(data.CategoryId),
		ProductName:        data.ProductName,
		ProductSelling:     data.ProductSelling,
		ProductImage:       data.ProductImage,
		VideoUrl:           data.VideoUrl,
		ProductStockUnitId: int64(data.ProductStockUnitId),
	})
	if err != nil {
		response.ResPonseError(c, err.Error())
		return
	}
	response.ResPonseSuccess(c, create)
}

func ProductOtherInfoCreate(c *gin.Context) {
	var data requset.ProductOtherInfoCreate
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResPonseError(c, err.Error())
		return
	}
	create, err := client.ProductOtherInfoCreate(c, &product.ProductOtherInfoCreateReq{
		AfterSalesStatus:    int64(data.AfterSalesStatus),
		WithoutReasonStatus: int64(data.WithoutReasonStatus),
		ProductStatus:       int64(data.ProductStatus),
		NoSaleStatus:        int64(data.NoSaleStatus),
	})
	if err != nil {
		response.ResPonseError(c, err.Error())
		return
	}
	response.ResPonseSuccess(c, create)
}
func ProductSpecificationsCreate(c *gin.Context) {
	var data requset.ProductSpecificationsCreate
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResPonseError(c, err.Error())
		return
	}
	create, err := client.ProductSpecificationsCreate(c, &product.ProductSpecificationsCreateReq{
		ProductId:         int64(data.ProductId),
		ProductSpec:       data.ProductSpec,
		ProductPackage:    data.ProductPackage,
		ImageSpec:         data.ImageSpec,
		SpecCode:          data.SpecCode,
		RetailPrice:       float32(data.RetailPrice),
		CostPrice:         float32(data.CostPrice),
		StockWarningValue: int64(data.StockWarningValue),
		Stock:             int64(data.Stock),
		Weight:            float32(data.Weight),
		SaleVolume:        int64(data.SaleVolume),
	})
	if err != nil {
		response.ResPonseError(c, err.Error())
		return
	}
	response.ResPonseSuccess(c, create)
}

func ProductList(c *gin.Context) {
	var data requset.ProductList
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResPonseError(c, err.Error())
		return
	}
	list, err := client.ProductList(c, &product.ProductListReq{
		Page: int64(data.Page),
		Size: int64(data.Size),
	})
	if err != nil {
		response.ResPonseError(c, err.Error())
		return
	}
	response.ResPonseSuccess(c, list)
}
